package org.linlinjava.litemall.wx.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallCategory;
import org.linlinjava.litemall.db.domain.LitemallCommodity;
import org.linlinjava.litemall.db.service.LitemallBannerService;
import org.linlinjava.litemall.db.service.LitemallCategoryService;
import org.linlinjava.litemall.db.service.LitemallCommodityService;
import org.linlinjava.litemall.db.service.LitemallSystemConfigService;
import org.linlinjava.litemall.wx.service.HomeCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类目服务
 */
@RestController
@RequestMapping("/wx/catalog")
@Validated
public class WxCatalogController {
    private final Log logger = LogFactory.getLog(WxCatalogController.class);

    @Autowired
    private LitemallCategoryService categoryService;
    @Autowired
    private LitemallBannerService bannerService;
    @Autowired
    private LitemallSystemConfigService configService;
    @Autowired
    private LitemallCommodityService commodityService;

    private final Integer PAGE_DEFAULT = 1 ;
    private final Integer PAGESIZE_DEFAULT = 6 ;

    /**
     * 分类详情
 *          分类类目ID。
 *             如果分类类目ID是空，则选择第一个分类类目。
 *             需要注意，这里分类类目是一级类目
     * @return 分类详情
     */
    @GetMapping("index")
    public Object index() {

        // 所有一级分类目录
        List<LitemallCategory> l1CatList = categoryService.queryL1();
//        所有子分类列表
        List<LitemallCategory> sub;
        for (LitemallCategory category : l1CatList) {
            sub = categoryService.queryByPid(category.getId());
            category.setChildCategory(sub);
        }
        return ResponseUtil.ok(l1CatList);
    }



  /**
     * @Description  所有分类数据  浩楠二级分类(旧)
     * @Author Riko  浩楠
     * @Date 2019/9/23
     **/
 /*   @GetMapping("all")
    public Object queryAll() {
        //优先从缓存中读取
        if (HomeCacheManager.hasData(HomeCacheManager.CATALOG)) {
            return ResponseUtil.ok(HomeCacheManager.getCacheData(HomeCacheManager.CATALOG));
        }
        // 所有一级分类目录
        List<LitemallCategory> l1CatList = categoryService.queryL1();
        //l1CatList = l1CatList.subList(0,3);
//        所有子分类列表
        List<LitemallCommodity> commodityList;
        List<LitemallCategory> sub;
        for (LitemallCategory category : l1CatList) {
            commodityList = commodityService.selectByCondition(1, 8, category.getId(), null, null, null, "commodity_price desc").getList();
            category.setChildCommodity(commodityList);
            sub = categoryService.queryByPid(category.getId());
            category.setChildCategory(sub);
        }
        Map<String, Object> data = new HashMap<>(2);
        data.put("categoryList", l1CatList);
        data.put("banner", banner());
        data.put("systemConstant", configService.listMail());
        //缓存数据
        HomeCacheManager.loadData(HomeCacheManager.CATALOG, data);
        return ResponseUtil.ok(data);
    }*/

    /**
     * 所有分类数据  魏来三级分类 仿照浩楠写.
     * @return
     */
    @GetMapping("all")
    public Object queryAlltest() {
        //优先从缓存中读取
        if (HomeCacheManager.hasData(HomeCacheManager.CATALOG)) {
            return ResponseUtil.ok(HomeCacheManager.getCacheData(HomeCacheManager.CATALOG));
        }
        Map<String, Object> data = new HashMap<>(2);
        data.put("categoryList", categoryL1List());
        data.put("banner", banner());
        data.put("systemConstant", configService.listMail());
        //缓存数据
        HomeCacheManager.loadData(HomeCacheManager.CATALOG, data);
        return ResponseUtil.ok(data);
    }

   // 显示 三级分类... L1级套L2级套L3级中查询商品
    private List<LitemallCategory> categoryL1List() {
        // 所有一级分类目录
        List<LitemallCategory> l1CatList = categoryService.queryL1();
        //所有子分类列表
        List<LitemallCommodity> commodityList;
        List<LitemallCategory> sub;
        //遍历得到L1级别的分类
        for (LitemallCategory categoryL1 : l1CatList) {
            //得到所有的 L2分类
            List<LitemallCategory> categoryL2List = categoryService.queryByPid(categoryL1.getId());
            categoryL1.setChildCategory(categoryL2List);
            for (LitemallCategory categoryL2 : categoryL2List) {
                sub= categoryService.queryByPid(categoryL2.getId());
                categoryL2.setChildCategoryL2(sub);
                commodityList =   commodityService.findByL1L2L3(PAGE_DEFAULT,PAGESIZE_DEFAULT,null,null,categoryL2.getId(),null,null,null).getList();
                categoryL1.setChildCommodity(commodityList);
               /*  List<Integer> intListL3 = new ArrayList<>();
               for (LitemallCategory categoryL3 : categoryL3List) {
                    intListL3.add(categoryL3.getId()); //L3级别   对应商品
                    //所有商品放入  分类L3级  遍历显示 (薛东霞遍历不出.改思路和方法..L2级显示商品)
                    commodityList = commodityService.selectByCondition(1, 6, null, intListL3, null, null, "commodity_price desc").getList();
                    categoryL3.setChildCommodity(commodityList);
                }*/
            }

        }
        return l1CatList;
    }

    /**
     * 当前分类栏目
     *
     * @param id 分类类目ID
     * @return 当前分类栏目
     */
    @GetMapping("current")
    public Object current(@NotNull Integer id) {
        // 当前分类
        LitemallCategory currentCategory = categoryService.findById(id);
        if(currentCategory == null){
            return ResponseUtil.badArgumentValue();
        }
        List<LitemallCategory> currentSubCategory = categoryService.queryByPid(currentCategory.getId());

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("currentCategory", currentCategory);
        data.put("currentSubCategory", currentSubCategory);
        return ResponseUtil.ok(data);
    }
    
    /**
     * @Description  获取轮播图
     * @Author Riko  浩楠
     * @Date 2019/9/23
     **/
    @GetMapping("banner")
    public Object banner() {
        return bannerService.querryAllBanner();
    }

    /**
     *  获取所有分类
     * @param pid 获取L1级 所有类别信息  默认PID 为 0
     * @Author Riko
     * @return
     */
    @GetMapping("/categoryAll")
    public Object categoryAll(@RequestParam(defaultValue = "0") Integer pid) {
        List<LitemallCategory> litemallCategories = categoryService.queryByPid(pid);
        Map<String, Object> data = new HashMap<>(1);
        data.put("categoryList", litemallCategories);
        return ResponseUtil.ok(data);
    }
}